package com.test.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.stereotype.Repository;

@Repository("baseDao")
public class BaseDao
{
	@Autowired
	JdbcTemplate jdbcTemplate = null;

	public List<Map<String, Object>> findMapList(String sql)
	{

		return jdbcTemplate.queryForList(sql);
	}

	public List<Map<String, Object>> findMapList(String sql, Object... params)
	{

		return jdbcTemplate.queryForList(sql, params);
	}

	public Boolean update(String sql, final Object... params)
	{
		return execute(sql, params);
	}

	public Boolean insert(String sql, Object... params)
	{
		return execute(sql, params);
	}
	
	private Boolean execute(String sql, final Object... params)
	{
		return jdbcTemplate.execute(sql, new PreparedStatementCallback<Boolean>()
		{
			@Override
			public Boolean doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException
			{
				for(int i = 0; i < params.length; i++)
				{
					ps.setObject(i + 1, params[i]);
				}
				return ps.execute();
			}
		});
	}
}
